package MathCore.Operands;

import MathCore.*;
import MathCore.Operators.TrigFunction;

/**
 *
 * @author Александр
 */
public class Complex extends Operand
{
    private Real re, im;
	public static final int RE_IM=0, ABS_ARG=1;

	public static Real.NumberFormat format;

    public Complex() 
    {
        re = new Real(0);
        im = new Real(0);
    }
    
    public Complex(Complex orig)
    {
        re = new Real(orig.Re());
        im = new Real(orig.Im());
    }

	public Complex(Real op1, Real op2, int method)
	{
		if(method == RE_IM)
		{
			re = new Real(op1);
			im = new Real(op2);
		}
		else if(method == ABS_ARG)
		{
			TrigFunction.convertAngleToRadians(op2);

			re = new Real(op2);
			re.cos();
			re.mul(op1);

			im = new Real(op2);
			im.sin();
			im.mul(op1);
		}
	}

    public Lexeme clone()
    {
        return new Complex(this);
    }

    public Real Re()
    {
        return re;
    }

    public Real Im()
    {
        return im;
    }

    public Real abs()
    {
        Real res = new Real(re);
        Real ims = new Real(im);
        res.sqr();
        ims.sqr();

        res.add(ims);
        res.sqrt();

        return res;
    }

    public Real arg()
    {
        Real angle = new Real(im);
        angle.atan2(re);
		TrigFunction.convertAngleFromRadians(angle);
        return angle;
    }

	public String toString()
	{
		if(format == null)
		{
			format = new Real.NumberFormat();
			format.maxwidth = 8;
			format.precision = 4;
		}
		
		String out = re.toString(format) + " ";
		Real im2 = new Real(im);
		im2.abs();
		if(im.sign == 0) out += "+";
		else out += "-";
		out += " i"+im2.toString(format);

		return out;
	}
}
